<template>
    <div class="big">
        <div class="center">
            <div class="imgput">
                <img src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" alt="">
            </div>
            <el-form class="ruleForm" :model="datas" :rules="myrules" ref="reset1" @keyup.enter.native="logins">
                <el-form-item prop="aname">
                    <el-input prefix-icon="iconfont icon-user" v-model="datas.aname"></el-input>
                </el-form-item>
                <el-form-item prop="apass">
                    <el-input type="password" prefix-icon="iconfont icon-3702mima" v-model="datas.apass"></el-input>
                </el-form-item>
                <el-form-item class="btnsub">
                    <el-button type="primary" @click.native.prevent="logins" >登陆</el-button>
                    <el-button type="info" @click="resets">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import {setToken} from "../../utils/auth";
    import {checkUser} from "../../network/admin";

    export default {
        name: "AdminLogin",
        data(){
            return{
                //默认的账号
                datas:{
                    aname:'admin',
                    apass:'user'
                },
                //表单验证
                //表单验证时候要与上面默认账号所对于的键 对应
                myrules:{
                    username: [
                        { required: true, message: '请输入名称', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    password:[
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
                    ]
                }
            }
        },
        methods:{
            resets(){
                this.$refs.reset1.resetFields()
            },
            //对表单预处理
            logins(){
                this.$refs.reset1.validate(async vals =>{
                    if (!vals) return;
                    checkUser(this.datas).then(res =>{
                        // console.log(res.code)
                        if(res.code === 1){
                            this.$message({
                                message: '登陆成功',
                                type: 'success'
                            });
                            setToken(res.content);
                            this.$router.push("/admin/home");
                        }else {
                            this.$message.error('登陆失败');
                        }
                    });
                })

            }
        }
    }
</script>

<style scoped>
    .big{
        height: 100%;
        width: 100%;
        background-color: #2b4b6b;
    }
    .center{
        width: 45%;
        height: 50%;
        background-color: white;
        border-radius: 5px;
        margin:auto;
        position: relative;
        top: 25%;
    }
    .imgput{
        width: 20%;
        position: relative;
        transform: translate(0, -50%);
        margin: auto;
        border: 1px solid #eee;
        padding: 10px;
        border-radius: 50%;
        box-shadow: 0 0 10px #ddd;
        background-color: #fff;
    }
    .imgput img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
    }
    .ruleForm{
        position: absolute;
        bottom: 10px;
        width: 100%;
        padding: 20px;
        box-sizing: border-box;
    }
    .btnsub{
        display: flex;
        justify-content: flex-end;
    }
</style>
